package com.ruge.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class QQEmailUtil {

    // QQ邮箱SMTP服务器配置
    private static final String SMTP_HOST = "smtp.qq.com";
    private static final String SMTP_PORT = "465";
    private static final String SMTP_AUTH = "true";
    private static final String SOCKET_FACTORY_CLASS = "javax.net.ssl.SSLSocketFactory";

    // 发件人信息（直接写死在代码中）
    // 注意：生产环境不建议这样做，存在安全风险
    private static final String SENDER_EMAIL = "1345528755@qq.com";      // 替换为你的QQ邮箱
    private static final String AUTHORIZATION_CODE = "cuydwpmeeuzugbaa"; // 替换为你的授权码

    /**
     * 发送文本邮件
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return 是否发送成功
     */
    public static boolean sendTextEmail(String to, String subject, String content) {
        // 配置邮件属性
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.auth", SMTP_AUTH);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY_CLASS);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);

        // 创建会话
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 使用内部写死的发件人信息进行认证
                return new PasswordAuthentication(SENDER_EMAIL, AUTHORIZATION_CODE);
            }
        });

        try {
            // 创建邮件消息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(SENDER_EMAIL));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(content);

            // 发送邮件
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            System.err.println("邮件发送失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 发送HTML格式邮件
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @param htmlContent HTML格式的邮件内容
     * @return 是否发送成功
     */
    public static boolean sendHtmlEmail(String to, String subject, String htmlContent) {
        // 配置邮件属性（与文本邮件相同）
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.auth", SMTP_AUTH);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY_CLASS);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);

        // 创建会话
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SENDER_EMAIL, AUTHORIZATION_CODE);
            }
        });

        try {
            // 创建邮件消息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(SENDER_EMAIL));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setContent(htmlContent, "text/html;charset=UTF-8");

            // 发送邮件
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            System.err.println("HTML邮件发送失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}
